﻿using System;
using Csla.Xaml;
using Microsoft.Practices.Prism.Regions;
using BizControl.Presentation.Infrastructure.Controls;
using Microsoft.Practices.Prism;
using System.ComponentModel.Composition;
using BizControl.Presentation.Infrastructure.Constants;
using System.ComponentModel;
using Microsoft.Practices.Prism.Commands;
using BizControl.Presentation.Infrastructure.Helpers;
using System.Collections.Generic;
using BizControl.Library;
using BizControl.Library.BusinessObjects.Models;
using System.Windows;
using System.Windows.Input;
using Microsoft.Win32;
using System.IO;
using BizControl.Library.BusinessObjects.Marks;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using BizControl.Presentation.Infrastructure.Properties;

namespace BizControl.Presentation.PersonMarkEditModule.ViewModels
{
  [Export]
  public class PersonMarkEditViewModel : ViewModel<PersonMarkBO>, INavigationAware, IRegionMemberLifetime, IConfirmNavigationRequest, IHeaderInfoProvider<string>
  {
    private readonly DelegateCommand _closeCommand;

    private IRegionManager _regionManager;

    [ImportingConstructor]
    public PersonMarkEditViewModel(IRegionManager regionManager)
    {
      _regionManager = regionManager;
      _closeCommand = new DelegateCommand(Close);
    }

    private void Refresh()
    {
      if (PersonMarkId != Guid.Empty)
      {
        DoRefresh(GetPersonMark);
      }
    }

    PersonMarkBO GetPersonMark()
    {
      return PersonMarkBO.GetPersonMark(PersonMarkId);
    }

    private void CreateNewRefresh()
    {
      DoRefresh(CreateNewPersonMark);
      _personMarkId = Model.MarkId;
    }

    PersonMarkBO CreateNewPersonMark()
    {
      return PersonMarkBO.NewPersonMark();
    }

    #region Handlers

    private void Close()
    {
      _regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, ViewNames.PersonMarksViewName, NavigationCompleted);
    }

    private void NavigationCompleted(NavigationResult result)
    {
    }

    #endregion

    #region Properties

    public ICommand CloseCommand
    {
      get { return _closeCommand; }
    }

    private Guid _personMarkId;
    private Guid PersonMarkId
    {
      get { return _personMarkId; }
      set
      {
        if (_personMarkId != value)
        {
          _personMarkId = value;
          Refresh();
        }
      }
    }

    #endregion

    #region INavigationAware Members

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
      return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
      PersonMarkId = Guid.Empty;
    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
      string markId = navigationContext.Parameters[NavigationParametersNames.IdName];
      string newPersonMark = navigationContext.Parameters[NavigationParametersNames.NewPersonMarkName];
      if (!string.IsNullOrEmpty(markId))
      {
        PersonMarkId = Guid.Parse(markId);
      }
      else if (!string.IsNullOrEmpty(newPersonMark))
      {
        CreateNewRefresh();
      }
    }

    #endregion

    #region IRegionMemberLifetime Members

    public bool KeepAlive
    {
      get { return true; }
    }

    #endregion

    #region IConfirmNavigationRequest Members

    public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
    {
      ConfirmationHelper.ConfirmNavigationRequestSaveDlg<PersonMarkBO>(this, continuationCallback);
    }

    #endregion

    #region IHeaderInfoProvider<string> Members

    public string HeaderInfo
    {
      get { return BizControl.Presentation.PersonMarkEditModule.Properties.Resources.PersonMarkEditHeaderInfo; }
    }

    #endregion
  }
}